//
//  ContactUsViewController.m
//  CenturyInterconnection
//
//  Created by Tian Wei You on 16/5/16.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "ContactUsViewController.h"
#import "SAMTextView.h"

@interface ContactUsViewController ()<UITextViewDelegate,UITextFieldDelegate>;

@property(nonatomic,strong)SAMTextView *enquiryTextView;
@property(nonatomic,strong)UITextField *nameTextField;
@property(nonatomic,strong)UITextField *postTextField;

@property(nonatomic,assign)BOOL canSave;

@end

@implementation ContactUsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeNaviButton];
    [self makeView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ContactUsPage"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ContactUsPage"];
}

-(void)makeNaviButton{
    
    self.title=@"CONTACT US";
    
    UIButton *forkButton=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(320), 0, ACTUAL_WIDTH(50), ACTUAL_HEIGHT(30))];
    [forkButton setTitle:@"Send" forState:0];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:0];
    
    leftButton.frame=CGRectMake(0, 0, ACTUAL_WIDTH(23), ACTUAL_HEIGHT(23));
    //    forkButton.backgroundColor=[UIColor yellowColor];
    [forkButton addTarget:self action:@selector(sendMessageToUs:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem*item1 = [[UIBarButtonItem alloc]initWithCustomView:forkButton];
    UIBarButtonItem*item= [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    
    self.navigationItem.leftBarButtonItem=item;
    self.navigationItem.rightBarButtonItem=item1;
}

#pragma mark 返回事件
-(void)comeBack{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)makeView{
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    _enquiryTextView=[[SAMTextView alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(10), ACTUAL_HEIGHT(10),KScreenWidth-ACTUAL_WIDTH(20), ACTUAL_HEIGHT(300))];
    _enquiryTextView.delegate=self;
//    _enquiryTextView.backgroundColor=[UIColor greenColor];
    _enquiryTextView.font=[UIFont systemFontOfSize:18];
    _enquiryTextView.placeholder=@"What can we do to help？";
    [self.view addSubview:_enquiryTextView];
    
    UIView *lineView1=[[UIView alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(10), ACTUAL_HEIGHT(320), KScreenWidth-ACTUAL_WIDTH(20), 2)];
    lineView1.backgroundColor=RGBCOLOR(234, 234, 234, 1);
    [self.view addSubview:lineView1];
    
    UIView *lineView2=[[UIView alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(187), ACTUAL_HEIGHT(320),2, ACTUAL_HEIGHT(40))];
    lineView2.backgroundColor=RGBCOLOR(234, 234, 234, 1);
    [self.view addSubview:lineView2];
    
    UIView *lineView3=[[UIView alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(10), ACTUAL_HEIGHT(360), KScreenWidth-ACTUAL_WIDTH(20), 2)];
    lineView3.backgroundColor=RGBCOLOR(234, 234, 234, 1);
    [self.view addSubview:lineView3];
    
    _nameTextField=[[UITextField alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(20), ACTUAL_HEIGHT(325), ACTUAL_WIDTH(155), ACTUAL_HEIGHT(30))];
//    _nameTextField.backgroundColor=[UIColor yellowColor];
    _nameTextField.placeholder=@"Name";
    [self.view addSubview:_nameTextField];
    
    _postTextField=[[UITextField alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(197), ACTUAL_HEIGHT(325), ACTUAL_WIDTH(155), ACTUAL_HEIGHT(30))];
//    _postTextField.backgroundColor=[UIColor purpleColor];
    _postTextField.placeholder=@"Email";
    [self.view addSubview:_postTextField];
}

-(void)sendMessageToUs:(UIButton*)sender{
    if (sender.selected==YES) {
        return;
    }else{
        sender.selected=YES;
        [self performSelector:@selector(timeEnough:) withObject:nil afterDelay:2.0];
        NSString*str=[self judgeCansave];
        
        if (_canSave) {
            // http://viprunningonline.com/api/index.php?  main_page=address_book_process
            
            NSString*aaa=@"main_page=contact_us";
            NSString*urlStr=[NSString stringWithFormat:@"%@%@",HTTP_ADDRESS,aaa];
            NSDictionary*params=@{@"contactname":self.nameTextField.text,@"email":self.postTextField.text,@"enquiry":self.enquiryTextView.text};
            HttpManager*manager=[[HttpManager alloc]init];
            
            [manager postDataFromNetworkWithUrl:urlStr parameters:params compliation:^(id data, NSError *error) {
                
                NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
                
                if ([number isEqualToString:@"1"]) {
                    [JRToast showWithText:@"Comment on success！"];
                    [self comeBack];
                }else{
                    [JRToast showWithText:data[@"status_message"]];
                }
            }];
            
        }else{
            [JRToast showWithText:str duration:2.0];
        }
        
    }
}

-(void)timeEnough:(NSTimer*)timer{
    UIButton *btn=(UIButton*)[self.view viewWithTag:666];
    btn.selected=NO;
    [timer invalidate];
    timer=nil;
}

#pragma mark 未填写警告

-(NSString*)judgeCansave{
    _canSave=YES;
    
    if (_enquiryTextView.text.length==0) {
        _canSave=NO;
        return @"Please fill in your comments or Suggestions！";
    }
    
    if (_nameTextField.text.length==0) {
        _canSave=NO;
        return @"Please fill in your name！";
    }
    
    if (_postTextField.text.length==0) {
        _canSave=NO;
        return @"Please fill in your email！";
    }
    return @"666";
}


#pragma mark 键盘收回
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
